const Model = require('./main')

class Topic extends Model {
    constructor(form={}) {
        super()
        this.id = form.id
        // views 是这个 topic 的浏览数目
        this.views = 0
        this.title = form.title || ''
        this.content = form.content || ''
        this.ct = Date.now()
        this.ut = this.ct
        this.userId = form.userId || ''
    }

    static get(id) {
        const m = super.get(id)
        m.views += 1
        m.save()
        return m
    }

    static fakeCreate(form) {
        const m = super.create(form)
        if (m === null) {
            const obj = {
                success: false,
                data: null,
                message: '用户名已经使用',
            }
            return obj
        } else {
            const obj = {
                success: true,
                data: m,
                message: '',
            }
            return obj
        }
    }

    replies() {
        const Reply = require('./reply')
        const ms = Reply.find('topic_id', this.id)
        return ms
    }
}

module.exports = Topic